package com.tfswue.ershoujiaoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tfswue.ershoujiaoyi.entity.Order;
import com.tfswue.ershoujiaoyi.mapper.OrderMapper;
import com.tfswue.ershoujiaoyi.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order createOrder(Order order) {
        order.setOrderTime(new Date());
        order.setStatus("PAID");
        orderMapper.insert(order);
        return order;
    }

    @Override
    public List<Order> listOrdersByBuyer(Long buyerId) {
        return orderMapper.selectList(
                new QueryWrapper<Order>().eq("buyer_id", buyerId)
        );
    }

    @Override
    public List<Order> listAll() {
        return orderMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Order getById(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public Order update(Order order) {
        orderMapper.updateById(order);
        return orderMapper.selectById(order.getId());
    }

    @Override
    public boolean delete(Long id) {
        return orderMapper.deleteById(id) > 0;
    }
}
